<template>
  <div style="height:100%">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="8">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.sysOrgCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="false" style="width: 200px" />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库" prop="busMainData.toWarehouseId">
            <WarehouseTreeSelect v-model="form.busMainData.toWarehouseId" :editable="form.busPageInfo.editable" style="width: 300px;" placeholder="选择仓库" @warehouseSelect="warehouseSelect" />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              size="mini"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="附档" prop="attachmentList">
            <FileUpload
              ref="fileUpload"
              :editable="form.busPageInfo.editable"
              :sup_this="sup_this"
              :file-limit="5"
              :is-direct-upload="true"
              list-type="list"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="入库产品" name="1">
        <stockin-detail ref="stockinDetail" :sup-form="thisForm" :stockin-details="form.stockinDetails" :warehouse-id="form.busMainData.toWarehouseId" :editable="form.busPageInfo.editable" @setDetails="setDetails" />
      </el-collapse-item>
    </el-collapse>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','STOCKIN_OTHER_ALL','STOCKIN_OTHER_CHANGE','STOCKIN_OTHER_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','STOCKIN_OTHER_ALL','STOCKIN_OTHER_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import stockinOther from '@/api/prod/StockinOther'
import StockinDetail from './stockinOtherDetail'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'
import WarehouseTreeSelect from '@/views/base/warehouse/module/WarehouseTreeSelect'

export default {
  components: { StockinDetail, WarehouseTreeSelect, OrgsetTreeSelect },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      thisForm: this,
      style: 'width: 184px',
      activeNames: ['1'],
      dialogInfo: {
        name: 'stockinOtherApproveMngFormDialog',
        title: '其它入库单审批',
        fullScreen: true,
        width: 500,
        height: 90
      },
      form: {
        stockinDetails: [],
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.busDate': [
          { required: true, message: '请选择业务时间', trigger: 'blur' }
        ],
        'busMainData.sysOrgCode': [
          { required: true, message: '请选择机构', trigger: 'blur' }
        ],
        'busMainData.toWarehouseId': [
          { required: true, message: '请选择仓库', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.$emit('afterMounted')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    viewApproveData(busId) {
      const data = { paramBean: { busId: busId }}
      stockinOther.toViewApprove(data).then(res => {
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.id, false, false)
      })
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        stockinOther.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.stockinDetails = res.data
        })
      }
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200)
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      stockinOther.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(null, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      stockinOther.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(mainId, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      stockinOther.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(id, false, false)
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          this.doAdd()
        }
      })
    },
    doAdd() {
      stockinOther.addOrEdit(this.form).then(res => {
        this.loading = false
        this.sup_this.init()
        this.resetForm()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      stockinOther.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      stockinOther.toChange(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.oldMainId, true, false)
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    cpySelectCallback(selectedRow, selectObj) {
      this.form.busMainData.otherSysOwnerCpy = ''
      this.form.busMainData.busOtherCpyName = ''
      if (selectedRow) {
        this.form.busMainData.otherSysOwnerCpy = selectedRow.cpyCode
        this.form.busMainData.busOtherCpyName = selectedRow.cpyLongName
      }
      // 更改供应商，清空明细
      this.form.stockinDetails = []
      selectObj.cancel()
    },
    orgSetSelectCallback(orgCode) {
      this.form.busMainData.otherSysOrgCode = orgCode
    },
    setDetails(details) {
      this.form.stockinDetails = details
    },
    warehouseSelect(warehouseId) {
      this.form.stockinDetails.forEach(tmpdetail => {
        tmpdetail.toStorelocationId = null
      })
    }
  }
}
</script>
